﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace yc.Site
{
	public class Constants
	{
		/// <summary>
		/// 获得客户端IP
		/// </summary>
		public static string ClientIP
		{
			get
			{
				var ip = HttpContext.Current.Request.UserHostAddress;
				if (ip == "::1") ip = "1.1.1.1";
				return ip;
			}
		}

		#region 系统常量
		/// <summary>
		/// 静态变量过期时间：分钟
		/// </summary>
		public static int StaticTimeOut { set; get; }

		/// <summary>
		/// Session用户变量名
		/// </summary>
		public static string SessionUser { get { return Config.WebSite.SessionUser; } }

		/// <summary>
		/// 系统配置文件
		/// </summary>
		public static CommonConfiguration Config { get { Load(); return _config; } }
		#endregion

		#region 用户常量
		public static string SXUACompatible { get { return Config.WebSite.XUACompatible; } }
		/// <summary>
		/// 网站标题
		/// </summary>
		public static string STitle { get { return Config.WebSite.Title; } }

		/// <summary>
		/// 网站语言
		/// </summary>
		public static string SLanguage { get { return Config.WebSite.Language; } }

		/// <summary>
		/// 
		/// </summary>
		public static string SRobots { get { return Config.WebSite.Robots; } }

		/// <summary>
		/// 网站作者
		/// </summary>
		public static string SAuthor { get { return Config.WebSite.Author; } }

		/// <summary>
		/// 网站版权
		/// </summary>
		public static string SCopyRight { get { return Config.WebSite.CopyRight; } }

		/// <summary>
		/// 
		/// </summary>
		public static string SMSSmartTagsPreventParsing { get { return Config.WebSite.MSSmartTagsPreventParsing; } }

		/// <summary>
		/// 
		/// </summary>
		public static string SImageToolBar { get { return Config.WebSite.ImageToolBar; } }

		/// <summary>
		/// 网站描述
		/// </summary>
		public static string SDescription { get { return Config.WebSite.Description; } }

		/// <summary>
		/// 网站关键字
		/// </summary>
		public static string SKeyWords { get { return Config.WebSite.KeyWords; } }

		/// <summary>
		/// 默认分页大小
		/// </summary>
		public static int DefaultPageSize { get { return Config.PageSetting.DefaultPageSize; } }
		#endregion

		#region 初始化数据
		static object aptypelock = new object();
		static DateTime executedate = DateTime.Now;//当前执行时间
		static CommonConfiguration _config;

		/// <summary>
		/// 刷新静态变量集合
		/// </summary>
		/// <param name="isforce">强制刷新</param>
		public static void Load(bool isforce = false)
		{
			if (isforce || _config == null || (DateTime.Now - executedate).TotalMinutes > StaticTimeOut)
			{
				lock (aptypelock)
				{
					_config = (ConfigurationManager.GetSection("commonConfig") as CommonConfiguration) ?? new CommonConfiguration();
					StaticTimeOut = _config.WebSite.StaticTimeOut;
					executedate = DateTime.Now;
				}
			}
		}
		#endregion

	}
}